package com.songlanyun.modules.servicegoods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.goods.entity.GoodsSku;
import com.songlanyun.modules.servicegoods.dao.ServiceGoodsDao;
import com.songlanyun.modules.servicegoods.dao.ServiceItemDao;
import com.songlanyun.modules.servicegoods.entity.ServiceGoods;
import com.songlanyun.modules.servicegoods.entity.ServiceItem;
import com.songlanyun.modules.servicegoods.service.ServiceGoodsService;
import com.songlanyun.modules.servicegoods.service.ServiceItemService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("serviceItemService")
public class ServiceItemServiceImpl extends ServiceImpl<ServiceItemDao, ServiceItem> implements ServiceItemService {

    @Override
    public ServiceItem create(ServiceItem entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public ServiceItem modify(ServiceItem entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        return null;
    }


    /**
     * 批量创建服务项目
     * @param serviceGoodsId
     * @param list
     */
    @Override
    public void branchCreate(Long serviceGoodsId, List<ServiceItem> list) {
        if(list.size()>0)
        {
            clearByGoodsId(serviceGoodsId);
            list.forEach(o->{
                o.setServiceGoodsId(serviceGoodsId);
            });
            /**
             * 检测单品只能被一个sku关联 sku更改成共用
             list.stream().forEach(goodsSku -> {
             validateGoodsSkuSingleGoodsRepeat(goodsSku);
             });
             */
            this.saveBatch(list);
        }
    }


    /**
     * 清除服务相目
     * @param serviceGoodsId
     */
    @Override
    public void clearByGoodsId(Long serviceGoodsId) {
        LambdaQueryWrapper<ServiceItem> wrapper =  new LambdaQueryWrapper<>();
        wrapper.eq(ServiceItem::getServiceGoodsId,serviceGoodsId);
        this.baseMapper.delete(wrapper);
    }

    @Override
    public List<ServiceItem> loadServiceItem(Long serviceGoodsId) {
        LambdaQueryWrapper<ServiceItem> wrapper =  new LambdaQueryWrapper<>();
        wrapper.eq(ServiceItem::getServiceGoodsId,serviceGoodsId);
        return this.list(wrapper);
    }
}
